/*
 *  multimedia_info.h
 *
 *  Library for reading metadata in multimedia files.
 *  Currently only MP3s are supported with MPLIB.  These functions
 *  are an Opqaue model for using multiple libraries, in order to support
 *  multiple media types.
 *
 * Support:
 *    MP3: Supported (Define: USE_MPLIB, -lmp)
 *    All Other Media: Not Yet, Supported
 *
 * Copyright (c) 2006 Karlan Thomas Mitchell<karlanmitchell@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, is prohibited except by
 * Karlan Thomas Mitchell.
 *
 * THIS SOFTWARE IS PROVIDED BY KARLAN THOMAS MITCHELL AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
 
/*
Here is a simple example, this library should be REALLY EASY to use!
*/


/*

#include <stdio.h>
#include <stdlib.h>
#include "multimedia_info.h"


int main(int argc, char * argv[]){

	if(argc == 1){
		printf("Please pass a media file as an argument!\n");
		return -1;
		}

	char buffer[1024];

	struct st_mminfo mminfo;

 	mminfo_load(&mminfo, argv[1]);


	mminfo_get(&mminfo, MM_TITLE, buffer, 1024);
	printf("Title:  %s\n", buffer);

	mminfo_get(&mminfo, MM_ALBUM, buffer, 1024);
	printf("Album:  %s\n", buffer);

	mminfo_get(&mminfo, MM_YEAR, buffer, 1024);
	printf("Year:   %s\n", buffer);

	mminfo_get(&mminfo, MM_GENRE, buffer, 1024);
	printf("Genre:  %s\n", buffer);

	mminfo_get(&mminfo, MM_TRACK, buffer, 1024);
	printf("Track:  %s\n", buffer);


	mminfo_getstr(&mminfo, "%bitrate kb/s %artist - %album - %title in %year", buffer, 1024);
	printf("String Example: %s\n", buffer);

	return 0;
	}

*/

#include "multimedia_info.h"



int mminfo_type(const char * filename){
	int file_type = MMTYPE_UNDEF;
	char * filetype_extension = strchr(filename, '.');

	if(filetype_extension == NULL)
		return file_type;


	*(filetype_extension)++;

	if(0){}
	#ifdef USE_MPLIB
	else if(strcasecmp(filetype_extension, "mp3") == 0)
		file_type = MMTYPE_MP3;
	#endif
	else if(strcasecmp(filetype_extension, "ogg") == 0)
		file_type = MMTYPE_OGG;
	else if(strcasecmp(filetype_extension, "m4a") == 0)
		file_type = MMTYPE_M4A;
	else if(strcasecmp(filetype_extension, "mpg") == 0)
		file_type = MMTYPE_MPG;
	else if(strcasecmp(filetype_extension, "mpeg") == 0)
		file_type = MMTYPE_MPG;
	else if(strcasecmp(filetype_extension, "mov") == 0)
		file_type = MMTYPE_MOV;
	else if(strcasecmp(filetype_extension, "wmv") == 0)
		file_type = MMTYPE_WMV;
	else if(strcasecmp(filetype_extension, "avi") == 0)
		file_type = MMTYPE_AVI;

	return file_type;
	}




int mminfo_load(struct st_mminfo * mminfo, const char * filename){
	mminfo->media_type = mminfo_type(filename);
	int media_type = mminfo->media_type;
	
	switch(mminfo->media_type){
		case MMTYPE_MP3:
			#ifdef USE_MPLIB
			mminfo->mpeg_info = mp_get_mpeg_header_from_file(filename);
			mminfo->mp3_info  = mp_get_tag_list_from_file(filename);
			if(mminfo->mpeg_info == NULL || mminfo->mp3_info == NULL)
				return -1;
			#endif
			break;
 		case MMTYPE_MPG:
 			#ifdef USE_MPLIB
 			mminfo->mpeg_info = mp_get_mpeg_header_from_file(filename);
			if(mminfo->mpeg_info == NULL)
				return -1;
 			#endif
 			break;
		default:
			#ifdef MMINFO_DEBUG
			printf("The following media type isn't supported (multimedia_info.h)\n");
			#endif
			return -1;
	}
	return 0;
	}


/*Converts my defines to API specific defines*/
int mminfo_resolv(int field, struct st_mminfo * mminfo){

	switch(mminfo->media_type){

		case MMTYPE_MP3:

			#ifdef USE_MPLIB
			switch(field){
				case MM_ARTIST:
					return MP_ARTIST;
				case MM_ALBUM:
					return MP_ALBUM;
				case MM_TITLE:
					return MP_TITLE;
				case MM_YEAR:
					return MP_YEAR;
				case MM_GENRE:
					return MP_GENRE;
				case MM_TRACK:
					return MP_TRACK;
				/*MM_BITRATE IS A MULTIMEDIA_INFO ABSTRACTION!*/
				
			}
			#endif

			break;


		default:
			#ifdef MMINFO_DEBUG
			printf("The following media type isn't supported (multimedia_info.h)\n");
			#endif
			return -1;
	}


	return -1;
	}




int mminfo_get(struct st_mminfo * mminfo, int field, char * buffer, size_t len){
	buffer[0] = 0;
	#ifdef USE_MPLIB
	id3_content * id3_tag;
	id3_text_content * id3_text;
	char * pntr;
	#endif


	switch(mminfo->media_type){
		case MMTYPE_MP3:
			#ifdef USE_MPLIB
			
			if(mminfo->mpeg_info == NULL || mminfo->mp3_info == NULL)
				return -1;

			switch(field){
				case MM_ARTIST:
				case MM_ALBUM:
				case MM_TITLE:
				case MM_YEAR:
				case MM_GENRE:
				case MM_TRACK:
 					id3_tag  = mp_get_content(mminfo->mp3_info->tag, mminfo_resolv(field, mminfo));
  					id3_text = mp_parse_text(id3_tag);
					if(id3_text != NULL)
  						strlcpy(buffer, id3_text->text, len);
					break;

				case MM_BITRATE:
					pntr = mp_get_str_bitrate(mminfo->mpeg_info);
					strlcpy(buffer, pntr, len);
					break;
			}

			#endif
			break;
		default:
			#ifdef MMINFO_DEBUG
			printf("The following media type isn't supported (multimedia_info.h)\n");
			#endif
			return -1;
	}

	return 0;
	}

int mminfo_getstr(struct st_mminfo * mminfo, char * fieldstr, char * buffer, size_t len){
	buffer[0] = 0;	

	char * cur_pntr = &(fieldstr[0]);	
	char * adv_pntr;
	char var_buf[1024];
	char info_buf[1024];
	char cpy_buf[1024];

	int rv = 0;

	while(*cur_pntr){

		if(*cur_pntr == '%' || *cur_pntr == '$' ){
			var_buf[0] = 0;
			*(cur_pntr++);
			if(*cur_pntr == '%' || *cur_pntr == 0)
				break;
			adv_pntr = strpbrk(cur_pntr, " %");
	
			/*This is an old way I did something, I'll keep it cuz its interesting
			  however, perhaps TODO with strstrreplace....?*/
			while(&*cur_pntr != &*adv_pntr &&  *cur_pntr){
				snprintf(cpy_buf, 1024, "%c", *cur_pntr);
				strlcat(var_buf, cpy_buf, len);
				*(cur_pntr)++;
				}

			if(strcasecmp(var_buf, "artist") == 0       || strcasecmp(var_buf, "a") == 0)
					rv = mminfo_get(mminfo, MM_ARTIST, info_buf, 1024);
			else if(strcasecmp(var_buf, "title") == 0   || strcasecmp(var_buf, "t") == 0)
					rv = mminfo_get(mminfo, MM_TITLE, info_buf, 1024);
			else if(strcasecmp(var_buf, "year") == 0    || strcasecmp(var_buf, "y") == 0)
					rv = mminfo_get(mminfo, MM_YEAR, info_buf, 1024);
			else if(strcasecmp(var_buf, "genre") == 0   || strcasecmp(var_buf, "g") == 0)
					rv = mminfo_get(mminfo, MM_GENRE, info_buf, 1024);
			else if(strcasecmp(var_buf, "album") == 0   || strcasecmp(var_buf, "A") == 0)
					rv = mminfo_get(mminfo, MM_ALBUM, info_buf, 1024);
			else if(strcasecmp(var_buf, "bitrate") == 0 || strcasecmp(var_buf, "b") == 0)
					rv = mminfo_get(mminfo, MM_BITRATE, info_buf, 1024);
			else if(strcasecmp(var_buf, "track") == 0   || strcasecmp(var_buf, "#") == 0)
					rv = mminfo_get(mminfo, MM_TRACK, info_buf, 1024);
			else
					rv = mminfo_get(mminfo, MM_ARTIST, info_buf, 1024);

			snprintf(cpy_buf, 1024, "%s", info_buf);
			strlcat(buffer, cpy_buf, len);

			continue;
			}

		snprintf(cpy_buf, 1024, "%c", *(cur_pntr));
		strlcat(buffer,cpy_buf, len);
		*(cur_pntr++);
		}
	
	return rv;
	}
